﻿using PaddleOCRSharp;
namespace PModelOCR
{
    public sealed class POCRService
    {
        // 静态字段，用于存储单例实例
        private static readonly Lazy<POCRService> lazyInstance = new Lazy<POCRService>(() => new POCRService());

        // OCR引擎实例
        private static PaddleOCREngine engine;

        // 私有构造函数，防止外部实例化
        private POCRService()
        {
            OCRParameter oCRParameter = new OCRParameter()
            {
                rec = true,
                det = true,
                
            };
            var basePath = Path.Combine(AppContext.BaseDirectory, "OCRModel");
            var config = new OCRModelConfig()
            {
                cls_infer = Path.Combine(basePath, "ch_ppocr_mobile_v2.0_cls_slim_infer"),
                det_infer = Path.Combine(basePath, "ch_PP-OCRv4_det_infer"),
                rec_infer = Path.Combine(basePath, "ch_PP-OCRv4_rec_infer"),
                keys = Path.Combine(basePath, "ppocr_keys.txt")
            };
            // 初始化OCR引擎
            engine = new PaddleOCREngine(config, oCRParameter);
        }

        // 公共静态属性，提供全局访问点
        public static POCRService Instance => lazyInstance.Value;

        // OCR识别方法
        public string OCRImgeText(string imagePath)
        {
            OCRResult ocrResult = engine.DetectText(imagePath);
            return ocrResult.Text;
        }
        // OCR识别方法
        public string OCRImgeText(byte[] imagebyte)
        {
            OCRResult ocrResult = engine.DetectText(imagebyte);
            return ocrResult.Text;
        }
    }
}
